package controllers;

import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.pageErreurConnexion;
import views.html.ConsoleAdmin.*;

import models.*;

/**
 * Manage a database of questions
 */
public abstract class AdminQuestion extends Controller {

	/**
	 * This result directly redirect to application home.
	 */
	public final static Result GO_HOME = redirect(routes.AdminQuestion
			.listQuestion(0, "id_question", "asc", ""));

	/**
	 * Handle default path requests, redirect to questions list
	 */
	public static Result indexQuestion() {
		if (Application.estAdmin()) {
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the paginated list of questions.
	 * 
	 * @param page
	 *            Current page number (starts from 0)
	 * @param sortBy
	 *            Column to be sorted
	 * @param order
	 *            Sort order (either asc or desc)
	 * @param filter
	 *            Filter applied on question names
	 */
	public static Result listQuestion(final int page, final String sortBy,
			final String order, final String filter) {
		if (Application.estAdmin()) {
			return ok(listQuestion.render(
					Question.page(page, 10, sortBy, order, filter), sortBy,
					order, filter));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'edit form' of a existing Question.
	 * 
	 * @param id
	 *            Id of the question to edit
	 */
	public static Result editQuestion(final Long id) {
		if (Application.estAdmin()) {
			final Form<Question> questionForm = form(Question.class).fill(
					Question.findById(id));
			return ok(editQuestion.render(id, questionForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'edit form' submission
	 * 
	 * @param id
	 *            Id of the question to edit
	 */
	public static Result updateQuestion(final Long id) {
		if (Application.estAdmin()) {
			final Form<Question> questionForm = form(Question.class)
					.bindFromRequest();
			if (questionForm.hasErrors()) {
				return badRequest(editQuestion.render(id, questionForm));
			}

			questionForm.get().update(id);
			flash("success", "Question "
					+ questionForm.get().getLibelleQuestion()
					+ " has been updated");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Display the 'new question form'.
	 */
	public static Result createQuestion() {
		if (Application.estAdmin()) {
			final Form<Question> questionForm = form(Question.class);
			return ok(createQuestion.render(questionForm));
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle the 'new question form' submission
	 */
	public static Result saveQuestion() {
		if (Application.estAdmin()) {
			final Form<Question> questionForm = form(Question.class)
					.bindFromRequest();
			if (questionForm.hasErrors()) {
				return badRequest(createQuestion.render(questionForm));
			}
			questionForm.get().save();
			flash("success", "Question "
					+ questionForm.get().getLibelleQuestion()
					+ " has been created");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

	/**
	 * Handle question deletion
	 */
	public static Result deleteQuestion(final Long id) {
		if (Application.estAdmin()) {
			Question.delete(id);
			flash("success", "Question has been deleted");
			return GO_HOME;
		} else
			return ok(pageErreurConnexion.render());
	}

}
